package com.rritw.webtools.util;

import java.util.regex.Pattern;

import com.rritw.webtools.common.Const;

/*
 * 类JSUtil.java的实现描述：TODO 类实现描述
 * @author ddp1j32 2015-4-2 上午10:02:21
 */
public abstract class JSUtil {

    private static final Pattern LINE_COMMENT       = Pattern.compile("(?<!:)\\/\\/[^\n]*");
    // private static final Pattern MULTI_LINE_COMMENT = Pattern.compile("/\\*.*\\*/");
    // private static final Pattern MULTI_LINE_COMMENT = Pattern.compile("\\/\\*(\\s|.)*?\\*\\/");
    private static final Pattern MULTI_LINE_COMMENT = Pattern.compile("\\/\\*([^\\*^\\/]*|[\\*^\\/*]*|[^\\**\\/]*)*\\*\\/");

    public static String removeJSComments2(String javaScript) {
        if (StringUtil.isEmpty(javaScript)) {
            return javaScript;
        }
        javaScript = javaScript.replaceAll("//.*\\\n", Const.EMPTY_STRING);
        javaScript = javaScript.replaceAll("/\\*.*\\*/", Const.EMPTY_STRING);
        return javaScript;
    }

    public static String removeJSComments(String javaScript) {
        if (StringUtil.isEmpty(javaScript)) {
            return javaScript;
        }
        javaScript = LINE_COMMENT.matcher(javaScript).replaceAll(Const.EMPTY_STRING);
        javaScript = MULTI_LINE_COMMENT.matcher(javaScript).replaceAll(Const.EMPTY_STRING);
        return javaScript;
    }
}
